package com.example.shiro.model.sys.service.impl;

import com.example.shiro.model.sys.entity.UserEntity;
import com.example.shiro.model.sys.entity.dto.UserInfoDto;
import com.example.shiro.model.sys.mapper.RoleAuthMapper;
import com.example.shiro.model.sys.mapper.UserMapper;
import com.example.shiro.model.sys.mapper.UserRoleMapper;
import com.example.shiro.model.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.velocity.app.event.implement.EscapeXmlReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 用户基本信息表 服务实现类
 * </p>
 *
 * @author jatej
 * @since 2020-04-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleAuthMapper roleAuthMapper;
    /**
     * 通过用户名获取用户登录信息
     * @param username
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserInfoDto getUserInfoByUsername(String username) {
        UserInfoDto userInfoByUsername = userMapper.getUserInfoByUsername(username);
        if( Objects.isNull( userInfoByUsername ) ){
            return null;
        }
        userInfoByUsername.setRoleNameList( userRoleMapper.getRoleNameById( userInfoByUsername.getId()  ) );
        userInfoByUsername.setAuthNameList( roleAuthMapper.getAuthNameByUserId( userInfoByUsername.getId()  ) );
        return userInfoByUsername;
    }

    /**
     * 通过用户名获取用户信息
     * @param username
     * @return
     */
    @Override
    public UserEntity getUserByUsername(String username) {
        return userMapper.getUserByUserName( username );
    }
}
